package com.yxdj.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.web.controller.BaseController;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.core.web.page.TableDataInfo;
import com.yxdj.common.log.annotation.Log;
import com.yxdj.common.log.enums.BusinessType;
import com.yxdj.common.security.annotation.InnerAuth;
import com.yxdj.common.security.annotation.RequiresPermissions;
import com.yxdj.user.api.domain.Engineer;
import com.yxdj.user.service.IEngineerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工程师信息Controller
 */
@Tag(name = "工程师信息管理")
@RestController
@RequestMapping("/engineer")
public class EngineerController extends BaseController {

    @Autowired
    private IEngineerService engineerService;

    /**
     * 查询工程师列表
     */
    @Operation(summary = "查询工程师列表")
    @RequiresPermissions("engineer:engineer:list")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "Engineer", description = "工程师查询条件") Engineer engineer) {
        startPage();
        LambdaQueryWrapper<Engineer> engineerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        engineerLambdaQueryWrapper.eq(engineer.getPhoneNumber() != null, Engineer::getPhoneNumber,engineer.getPhoneNumber());
        engineerLambdaQueryWrapper.like(engineer.getName() != null, Engineer::getName,engineer.getName());
        List<Engineer> list = engineerService.list(engineerLambdaQueryWrapper);
        return getDataTable(list);
    }


    /**
     * 获取工程师详细信息
     */
    @Operation(summary = "获取工程师详细信息")
    @RequiresPermissions("engineer:engineer:query")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(engineerService.getById(id));
    }

    /**
     * 新增工程师
     */
    @Operation(summary = "新增工程师")
    @RequiresPermissions("engineer:engineer:add")
    @Log(title = "工程师信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Engineer engineer) {
        return toAjax(engineerService.save(engineer));
    }

    /**
     * 修改工程师信息
     */
    @Operation(summary = "修改工程师信息")
    @RequiresPermissions("engineer:engineer:edit")
    @Log(title = "工程师信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Engineer engineer) {
        return toAjax(engineerService.updateById(engineer));
    }

    /**
     * 删除工程师
     */
    @Operation(summary = "删除工程师")
    @RequiresPermissions("engineer:engineer:remove")
    @Log(title = "工程师信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(engineerService.removeBatchByIds(ids));
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用工程师登录的接口")
    @PostMapping("/app/login/{phone}")
    public R<Engineer> login(@PathVariable(name = "phone") String phone){
        LambdaQueryWrapper<Engineer> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(Engineer::getPhoneNumber, phone);
        Engineer engineer = engineerService.getOne(userLambdaQueryWrapper);
        return R.ok(engineer);
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用工程师注册的接口")
    @PostMapping("/app/register")
    public R<Engineer> register(@RequestBody Engineer engineer){
        engineerService.save(engineer);
        return R.ok(engineer);
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用根据id获取工程师信息")
    @GetMapping("/getEngineerById/{id}")
    public R<Engineer> getEngineerById(@PathVariable(name = "id") Long id) {
        Engineer engineer = engineerService.getById(id);
        return R.ok(engineer);
    }
}
